// pages/server_end/order_check/order_check.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: "",
    id: 0,
    imgs: [],
    picList: [],
    remark: '',
	captcha:'',
	countdown: 60,
	isDisabled: false,
	btnText:"发送验证码",
	mobile:'',
	insect_name:'',
	insect_hazard:'',
	insect_remark:''
  },
  onLoad(options) {
	  console.log('options.mobile:'+options.phone)
    this.setData({
      id: options.id,
	  mobile:options.phone
    })
  },
  onReady() {},
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
        }
      }
    })
  },
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {},
 // 倒计时函数
    countdown: function() {
      let that = this;
      let countdown = that.data.countdown;
      
      // 设置定时器
      let timer = setInterval(function() {
        countdown--;
        that.setData({
          btnText: countdown + '秒后重试'
        });
        
        // 当倒计时结束时
        if (countdown <= 0) {
          clearInterval(timer);
          that.setData({
            btnText: '发送验证码',
            isDisabled: false
          });
        }
      }, 1000);
    },
	smssend(){
		// var id = e.currentTarget.dataset.id;
		
		var that = this;
		if(that.data.isDisabled==true)
		{
			wx.showToast({title:"请稍后再试！",icon:"none"});
			return false;
		}
		 this.setData({
		      isDisabled: true
		    });
	   this.countdown();
		console.log('isDisabled：'+that.data.isDisabled);
		let param = {
		  scoure: 'wxapp',
		  token:that.data.token,
		  mobile:that.data.mobile,
		   event:'notice',
		}
		wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
		request.request(url.smssend, param, url.REQUEST_METHOD_POST).then((res) => {
		  wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
		  wx.showToast({
		    title: res.data.msg,
		    icon:"none"
		  })
		  if(res.data.code==1){
		   }
		})  	
	},
	smscheck(that){
		if(that.data.captcha=='')
		{
			wx.showToast({title:"请输入验证码！",icon:"none"});
			return false;
		}
		let param = {
		  scoure: 'wxapp',
		  token:that.data.token,
		  mobile:that.data.mobile,
		  event:'notice',
		  captcha:that.data.captcha
		}
		wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
		request.request(url.smscheck, param, url.REQUEST_METHOD_POST).then((res) => {
		  wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
		console.log(res.data);
		  if(res.data.code==1){
			  if (!that.data.imgs.length) {
			    wx.showToast({
			      title: '请上传施工图片',
			      icon: 'none'
			    })
			    return false;
			  }
			  wx.requestSubscribeMessage({
			    tmplIds: [
			      'YMSwKL5I2Efw96anh8E9yfr7_lhJptrNQBUr5u309uI'
			    ],
			    success: (res) => {
			      that.checkOrder();
			    },
			    fail: (res) => {
			      that.checkOrder();
			    },
			  })
			  return true;
		   }else
		   {
			   wx.showToast({
			     title: res.data.msg,
			     icon:"none"
			   })
			   return false;
		   }
		})  	
	},
  //写入备注信息
  changeinsect_name(e) {
    this.setData({
      insect_name: e.detail.value
    })
  },
  changeinsect_hazard(e) {
    this.setData({
      insect_hazard: e.detail.value
    })
  },
  changeinsect_remark(e) {
    this.setData({
      insect_remark: e.detail.value
    })
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var picList = this.data.picList;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    picList.splice(index, 1);
    this.setData({
      imgs: imgs,
      picList: picList
    });
  },
  //提交图片
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 9) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function () {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    var count = 9 - imgs.length;
    if (count < 1) {
      return false;
    }

    wx.chooseImage({
      count: count, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        if (res.errMsg == "chooseImage:ok") {
          wx.showLoading({ //显示loding提示框
            title: '正在上传中...',
          })
          let picList = that.data.picList
          var imgs = that.data.imgs;
          res.tempFilePaths.forEach((item, index) => {
            imgs.push(item)
            that.setData({
              imgs: imgs,
            });
            wx.uploadFile({
              url: url.HttpServerHostAddress + url.uploadpic,
              filePath: item,
              name: 'file',
              formData: {
                token: that.data.token
              },
              success: function (res) {
                let ress = JSON.parse(res.data)
                if (ress.code == 1) {
                  var picList = that.data.picList;
                  picList.push(JSON.parse(res.data).data.url);
                  that.setData({
                    picList: picList,
                  })
                  console.log(picList);
                } else {
                  wx.showToast({
                    title: JSON.parse(res.data).msg,
                    icon: 'none',
                    duration: 3000
                  })
                  return;
                }
              },
              fail: res => {
                console.log(res)
              }
            })
          })
          wx.hideLoading(); //隐藏loading提示框
        }
      }
    });
  },
  checkOrder(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      id: that.data.id,
      // images: that.data.picList,
      insect_name: that.data.insect_name,
	  insect_hazard:that.data.insect_hazard,
	  insect_remark:that.data.insect_remark
    }
	console.log(url.orderdiagnose);
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.orderdiagnose, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      });
      if (res.data.code == 1) {
        setTimeout(function () {
         wx.navigateTo({
           url: '../index_expert/index_expert'
         })
        }, 1500)
        return false;
      }
    })
  },
  //提交完工信息
  apply() {
    var that = this;
    // this.smscheck(that);
   wx.requestSubscribeMessage({
     tmplIds: [
       'YMSwKL5I2Efw96anh8E9yfr7_lhJptrNQBUr5u309uI',
	   '0fHW_V0mpcRkwvflDCdY9393_a-p-0xW08pRO8pNSMk',
     'Z-OFKRd2aAOX20azHmd6D30cXIdIGaW1jX-QWEHieHc' //拒绝通知(入驻拒绝)通知服务人员

     ],
     success: (res) => {
       that.checkOrder();
     },
     fail: (res) => {
       that.checkOrder();
     },
   })
	
    
  }
})